package action.admin.account;

import beans.Account;
import dao.CheckAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class updateAcc extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String acc_id = request.getParameter("acc_id");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String full_name = request.getParameter("full_name");
            String gender = request.getParameter("gender");
            String address = request.getParameter("address");
            String phone = request.getParameter("phone");
            String email = request.getParameter("email");
            String roll = request.getParameter("roll");
            Account account = new Account();
            account.setUser_name(username);
            account.setPass_word(password);
            account.setFull_name(full_name);
            account.setGender(gender);
            account.setAddress(address);
            account.setPhone(phone);
            account.setEmail(email);
            account.setRoll(roll);
            account.setAcc_id(acc_id);
            CheckAccount ck = new CheckAccount();
            String msg = "";
            String error = "";
            if (!full_name.equals("") && full_name != null) {
                if (!ck.checkUsername(username, acc_id)) {
                    if (ck.updateAccount(account)) {
                        msg = "Update successful !";
                        request.setAttribute("msg", msg);
                        request.setAttribute("roll", roll);
                        RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=account");
                        rd.forward(request, response);
                    } else {
                        error = "Update fail !";
                        request.setAttribute("error", error);
                        request.setAttribute("acc_id", acc_id);
                        request.setAttribute("username", username);
                        request.setAttribute("password", password);
                        request.setAttribute("full_name", full_name);
                        request.setAttribute("gender", gender);
                        request.setAttribute("address", address);
                        request.setAttribute("phone", phone);
                        request.setAttribute("email", email);
                        request.setAttribute("roll", roll);
                        RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=edituser");
                        rd.forward(request, response);
                    }
                } else {
                    error = "Update fail - user_name has exist !";
                    request.setAttribute("error", error);
                    request.setAttribute("acc_id", acc_id);
                    request.setAttribute("username", username);
                    request.setAttribute("password", password);
                    request.setAttribute("full_name", full_name);
                    request.setAttribute("gender", gender);
                    request.setAttribute("address", address);
                    request.setAttribute("phone", phone);
                    request.setAttribute("email", email);
                    request.setAttribute("roll", roll);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=edituser");
                    rd.forward(request, response);
                }
            } else {
                error = "Update fail - Insert full name";
                request.setAttribute("error", error);
                request.setAttribute("acc_id", acc_id);
                request.setAttribute("username", username);
                request.setAttribute("password", password);
                request.setAttribute("full_name", full_name);
                request.setAttribute("gender", gender);
                request.setAttribute("address", address);
                request.setAttribute("phone", phone);
                request.setAttribute("email", email);
                request.setAttribute("roll", roll);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=edituser");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
